package com.ericsson.entironment.bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ericsson.common.db.DatabaseMng;
import com.ericsson.common.exception.SmafException;

public class HouseBean
{
	private String hsname;
	private String hsaddreess;
	private String hsid=null;
	private int terminal;
	private static Log log=LogFactory.getLog(HouseBean.class);
	public void loadByName(String name)
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
	
			sql="select * from ema_greenhouse where hsname='"+name+"'";
			
			rst=stmt.executeQuery(sql);
			if (rst.next())
			{//记录存在
				this.hsaddreess=rst.getString("hsaddress");
				this.hsid=rst.getString("hsid");
				this.hsname=rst.getString("hsname");
				this.terminal=getTerminalNumber();
				
			}
			else
			{//没有匹配的记录
				this.hsid=null;
			}
			

		}
		catch (Exception e)
		{
			log.error("载入大棚数据失败，执行数据库操作失败 sql["+sql+"]",e);
			
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
	}
	public void loadById(String id)
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
	
			sql="select * from ema_greenhouse where hsid='"+id+"'";
			
			rst=stmt.executeQuery(sql);
			if (rst.next())
			{//记录存在
				this.hsaddreess=rst.getString("hsaddress");
				this.hsid=rst.getString("hsid");
				this.hsname=rst.getString("hsname");
				this.terminal=getTerminalNumber();
			}
			else
			{//没有匹配的记录
				this.hsid=null;
			}
			

		}
		catch (Exception e)
		{
			log.error("载入大棚数据失败，执行数据库操作失败 sql["+sql+"]",e);
			
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
	}
	private int getTerminalNumber()
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
	
			sql="select count(*) from ema_terminalinstall where hsid='"+this.hsid+"'";
			
			rst=stmt.executeQuery(sql);
			if (rst.next())
			{	
				return rst.getInt(1);
			}			else
			{//没有匹配的记录
				return 0;
			}
			

		}
		catch (Exception e)
		{
			log.error("载入大棚数据失败，执行数据库操作失败 sql["+sql+"]",e);
			
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		return 0;
	}
	public void delete()
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";

		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
	
			sql="delete from ema_greenhouse where hsid='"+this.hsid+"'";
			
			stmt.executeUpdate(sql);

		}
		catch (Exception e)
		{
			log.error("无法删除大棚数据，执行数据库操作失败 sql["+sql+"]",e);
			
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
	}
	public void save() throws SmafException
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		String id;
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			if (this.hsid==null)
			{//新增
				this.hsid=UUID.randomUUID().toString();
				sql="insert into ema_greenhouse(hsid,hsname,hsaddress) values ('"+this.hsid+"','"+this.hsname+"','"+this.hsaddreess+"')";
			}
			else
			{//修改
				sql="update ema_greenhouse set hsname='"+this.hsname+"',hsaddress='"+this.hsaddreess+"' where hsid=-'"+this.hsid+"'";
				
			}
			stmt.executeUpdate(sql);

		}
		catch (Exception e)
		{
			log.error("无法保存大棚数据，执行数据库操作失败 sql["+sql+"]",e);
			throw new SmafException(e);
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
	}
	public String getHsaddreess()
	{
		return hsaddreess;
	}
	public void setHsaddreess(String hsaddreess)
	{
		this.hsaddreess = hsaddreess;
	}
	public String getHsname()
	{
		return hsname;
	}
	public void setHsname(String hsname)
	{
		this.hsname = hsname;
	}
	public int getTerminal()
	{
		return terminal;
	}
	public void setTerminal(int terminal)
	{
		this.terminal = terminal;
	}
	public String getHsid()
	{
		return hsid;
	}
	public boolean hasAlert()
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
	
			sql="select * from ema_terminal as term,ema_terminalinstall as ins where ins.hsid='"
				+this.hsid+"' and term.trmstatus=2 and term.trmsequence=ins.trmsequence";
			
			rst=stmt.executeQuery(sql);
			if (rst.next())
			{	
				return true;
			}
			else	
			{//没有匹配的记录
				return false;
			}
			

		}
		catch (Exception e)
		{
			log.error("载入大棚数据失败，执行数据库操作失败 sql["+sql+"]",e);
			
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		return false;
	}
	
}
